/**
 * 
 */
package dataPersistence.dto;




import hitExceptions.InvalidDataException;

import java.util.Date;

import coreModel.Barcode;
import coreModel.Product;
import coreModel.Supply;
import coreModel.Unit;

/**
 * Inherits from DataTransferObject
 *
 */
public class ProductDTO implements DataTransferObject<Product>
{
	
	private String barcode;
	private Date entryDate;
	private String description ;
	private int shelfLife;
	private int threeMonthSupply;
	private float productSupply ;
	private String productSupplyUnit;

	
	public ProductDTO(Product p)
	{
		convertFromModelObject(p);
	}
	
	public ProductDTO()
	{
		
	}
	
	
	@Override
	public Product convertToModelObject()
	{
		Product p=null;
		try
		{
			 p = new Product(description, new Barcode(barcode));
			 java.util.Date theDate = new Date(entryDate.getTime());
			 p.setCreationDate(theDate);
			 p.setShelfLife(shelfLife);
			 p.setSupply(new Supply(productSupply, toUnit(productSupplyUnit)));
			 return p;
		} 
		catch (InvalidDataException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
		
	}
	
	@Override
	public void convertFromModelObject(Product p)
	{
		this.barcode = p.getBarcode().toString();
		this.entryDate=p.getCreationDate();
		this.description=p.getDescription();
		this.shelfLife=p.getShelfLife();
		this.productSupply=p.getSupply().getSupplySize();
		this.productSupplyUnit=p.getSupply().getSupplyUnit().toString();
		
	}

	/**
	 * @return the barcode
	 */
	public String getBarcode() {
		return barcode;
	}

	/**
	 * @param barcode the barcode to set
	 */
	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	/**
	 * @return the entryDate
	 */
	public Date getEntryDate() {
		return entryDate;
	}

	/**
	 * @param entryDate the entryDate to set
	 */
	public void setEntryDate(Date entryDate) {
		this.entryDate = entryDate;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the shelfLife
	 */
	public int getShelfLife() {
		return shelfLife;
	}

	/**
	 * @param shelfLife the shelfLife to set
	 */
	public void setShelfLife(int shelfLife) {
		this.shelfLife = shelfLife;
	}

	/**
	 * @return the threeMonthSupply
	 */
	public int getThreeMonthSupply() {
		return threeMonthSupply;
	}

	/**
	 * @param threeMonthSupply the threeMonthSupply to set
	 */
	public void setThreeMonthSupply(int threeMonthSupply) {
		this.threeMonthSupply = threeMonthSupply;
	}

	/**
	 * @return the productSupply
	 */
	public float getProductSupply() {
		return productSupply;
	}

	/**
	 * @param f the productSupply to set
	 */
	public void setProductSupply(float f) {
		this.productSupply = f;
	}

	/**
	 * @return the productSupplyUnit
	 */
	public String getProductSupplyUnit() {
		return productSupplyUnit;
	}

	/**
	 * @param productSupplyUnit the productSupplyUnit to set
	 */
	public void setProductSupplyUnit(String productSupplyUnit) {
		this.productSupplyUnit = productSupplyUnit;
	}
	
	public static Unit toUnit (String s)
	{

		if(s.equalsIgnoreCase("COUNT"))
		{
			return Unit.COUNT;
		}
		else if(s.equalsIgnoreCase("POUNDS"))
		{
			return Unit.POUNDS;
		}
		else if(s.equalsIgnoreCase("OUNCES"))
		{
			return Unit.OUNCES;
		}
		else if(s.equalsIgnoreCase("GRAMS"))
		{
			return Unit.GRAMS;
		}
		else if(s.equalsIgnoreCase("KILOGRAMS"))
		{
			return Unit.KILOGRAMS;
		}
		else if(s.equalsIgnoreCase("GALLONS"))
		{
			return Unit.GALLONS;
		}
		else if(s.equalsIgnoreCase("QUARTS"))
		{
			return Unit.QUARTS;
		}
		else if(s.equalsIgnoreCase("PINTS"))
		{
			return Unit.PINTS;
		}
		else if(s.equalsIgnoreCase("FLUID_OUNCES"))
		{
			return Unit.FLUID_OUNCES;
		}
		else if(s.equalsIgnoreCase("LITERS"))
		{
			return Unit.LITERS;
		}
		else 
		{
			return null;
		}
	}


	
	

}
